/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tkgarment.system.dao;

import com.tkgarment.system.db.Database;
import com.tkgarment.system.domain.Department;
import com.tkgarment.system.factory.DAOFactory;
import com.tkgarment.system.inf.IDAO;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author zraj
 */
public class DepartmentDAO extends AbstractDAO implements IDAO<Department,String> {

    public DepartmentDAO() {
    }

    public DepartmentDAO(Database con) {
        this.setCon(con);
    }

    @Override
    public List<Department> getAll() throws Exception {

        String sql = "select * from department order by department_name";
        this.setCon(DAOFactory.creatConnection());
        List<Map<String, Object>> results = this.getCon().queryList(sql);

        List<Department> depts = new ArrayList<>();

        for (Map<String, Object> map : results) {
            Department dept = new Department();
            dept.setDepartmentID(map.get(Department.field_department_id).toString());
            dept.setDepartmentName(map.get(Department.field_department_name).toString());
            depts.add(dept);
        }

        return depts;

    }

    public Department getDeptByID(String deptID)throws Exception {
        String sql = "select * from department where " + Department.field_department_id + " = ?";
        if (this.getCon() == null) {
            this.setCon(DAOFactory.creatConnection());
        }
        
        Department d = new Department();
        Map<String , Object> map = this.getCon().querySingle(sql, deptID);
       // System.out.println(deptID);
         d.setDepartmentID((String)map.get(Department.field_department_id));
         d.setDepartmentName((String)map.get(Department.field_department_name));
        return d;
    }

    
    
    @Override
    public int updateSingle(Department t, String id) throws Exception {
         String sql = "update department set "+ Department.field_department_id +" = ?,"
                 + "   "+ Department.field_department_name +" = ? where " + Department.field_department_id + " = ?";
        if (this.getCon() == null) {
            this.setCon(DAOFactory.creatConnection());
        }

        return this.getCon().update(sql,t.getDepartmentID(),t.getDepartmentName(),id);
    }
    
  
    
    
    

    @Override
    public int deleteSingle(Department t) throws Exception {
        String sql = "delete from department where department_id = ?";
        if (this.getCon() == null) {
            this.setCon(DAOFactory.creatConnection());
        }
        String checkUse = "select * from emp_master where emp_master.department_id = ?";
        System.out.println(checkUse);
        List<Map<String,Object>> result = this.getCon().queryList(checkUse, t.getDepartmentID());
        
        if (result.size() > 0 ) {
            return 99;
        }
        
        return this.getCon().update(sql, t.getDepartmentID());
    }

    @Override
    public int insertSingle(Department t) throws Exception {
        String sql = "insert into department values(?,?)";
        if (this.getCon() == null) {
            this.setCon(DAOFactory.creatConnection());
        }
        if (checkExist(t.getDepartmentID()) == false) {
          return this.getCon().add(sql, t.getDepartmentID(),t.getDepartmentName());    
        }else
        {
           throw new Exception("มีรหัสหน่วยงานนี้แล้ว");
        }
        
    }
    
    public List<Department> searchDepartment(String searchString )throws Exception{
        String sql = "select * from department where 1 = 1";
        if (StringUtils.isNotEmpty(searchString)) {
            sql = sql + " and department_id like '%" + searchString + "%' or department_name like '%" + searchString + "%'";
        }
        System.out.println(sql);
        if (this.getCon() == null) {
            this.setCon(DAOFactory.creatConnection());
        }
        
        List<Map<String, Object>> results = this.getCon().queryList(sql);

        List<Department> depts = new ArrayList<>();

        for (Map<String, Object> map : results) {
            Department dept = new Department();
            dept.setDepartmentID(map.get(Department.field_department_id).toString());
            dept.setDepartmentName(map.get(Department.field_department_name).toString());
            depts.add(dept);
        }

        return depts;
    }
    
    public boolean checkExist(String deptID) 
    {
        if (this.getCon() == null) {
            this.setCon(DAOFactory.creatConnection());
        }
         String sql = "select * from department where department_id = ?";
         List<Map<String, Object>> result = this.getCon().queryList(sql, deptID);
         
         if (result.size() > 0) {
            return true;
        }else{
           return false;     
         }
        
        
        
    }

    @Override
    public Department getById(String id) throws Exception {
        String sql = "select * from department where "  + Department.field_department_id + " = ?";
        
        if (this.getCon() == null) {
            this.setCon(DAOFactory.creatConnection());
        }
        
       Map<String, Object> map= this.getCon().querySingle(sql,id);

        List<Department> depts = new ArrayList<>();

      
            Department dept = new Department();
            dept.setDepartmentID(map.get(Department.field_department_id).toString());
            dept.setDepartmentName(map.get(Department.field_department_name).toString());
           
        return dept;
    }

}
